#!/bin/bash

set -eux # -x for verbose logging to juju debug-log

szPackageList="tomcat7 default-jdk exuberant-ctags unzip zip"
# zip and unzip is for hacking the source.war.

# relevant web links.

juju-log "III Getting any config parameters."

szDefaultPkgOpengrokProtocol="http"
szDefaultPkgOpengrokPath="http://java.net/projects/opengrok/downloads/download/"
szDefaultPkgOpengrokFilename="opengrok-0.11.1.tar.gz"
szDefaultPkgOpengrokMD5sum=""

szPkgOpengrokProtocol=`config-get pkg_og_protocol`
szPkgOpengrokPath=`config-get pkg_og_path`
szPkgOpengrokFilename=`config-get pkg_og_filename`
szPkgOpengrokMD5sum=`config-get pkg_og_md5sum`

if [ -z "$szPkgOpengrokProtocol" ]
then
 szPkgOpengrokProtocol="$szDefaultPkgOpengrokProtocol"
fi

if [ -z "$szPkgOpengrokPath" ]
then
 szPkgOpengrokPath="$szDefaultPkgOpengrokPath"
fi

if [ -z "$szPkgOpengrokFilename" ]
then
 szPkgOpengrokFilename="$szDefaultPkgOpengrokFilename"
fi

if [ -z "$szPkgOpengrokMD5sum" ]
then
 szPkgOpengrokMD5sum="$szDefaultPkgOpengrokMD5sum"
fi

##############################
# INSTALL SYSTEM DEPENDENCIES
#############################
if [ ! -z "$szPackageList" ];
then
  juju-log "III Installing $szPackageList via apt-get"

  apt-get -y install $szPackageList
fi

if  [ "$szPkgOpengrokProtocol" == "http" ];
then
  juju-log "III wget ${szPkgOpengrokPath}${szPkgOpengrokFilename}"
  wget ${szPkgOpengrokPath}${szPkgOpengrokFilename}
  szPkgOpengrokPath="./"
fi

szMd5sum=`md5sum ${szPkgOpengrokPath}${szPkgOpengrokFilename} | awk '{print \$1}'`
if  [ "$szMd5sum" != "$szPkgOpengrokMD5sum" ];
then
  juju-log "EEE MD5 sums does not match: "
  juju-log "    Should be: $szPkgOpengrokMD5sum"
  juju-log "    was......: $szMd5sum"
else
  szBaseCharmPath=`pwd`
  juju-log "III Setting up Opengrok"
  mkdir -p /var/opengrok/data
  mkdir -p /var/opengrok/etc
  mkdir -p /var/opengrok/src
  juju-log "III Hacking the tomcat dir name so Opengrok deploy will run."
  pushd /var/lib
  ln -s tomcat7 tomcat6
  popd

  # TODO C if path starts with '.' then get pwd and prepend that.
  szAbsolutePathAndFile="${szBaseCharmPath}/${szPkgOpengrokPath}${szPkgOpengrokFilename}"
  pushd /opt
  tar -zxvf ${szAbsolutePathAndFile}
  # TODO V make the dirname a config parm.
  ln -s opengrok-0.11.1 opengrok
  # TODO Configure everything, tomcat and opengrok.
  
  cd opengrok
  cd lib
  mkdir tmp
  cd tmp
  unzip ../source.war
  cp ${szBaseCharmPath}/opengrok_web.xml WEB-INF/web.xml
  zip -r ../source.war .
  cd ..
  rm -rf tmp
  cd ..
  cd bin
  ./OpenGrok deploy
  popd

  juju-log "III Configuring the tomcat."
  cp config_server.xml /etc/tomcat7/server.xml 
  cp config_tomcat-users.xml /etc/tomcat7/tomcat-users.xml 

  # TODO make the root path a var, also make the /var/lib/tomcat7 a var.
  if [ ! -d "/var/lib/tomcat7/webapps/ROOT/WEB-INF" ];
  then
    mkdir /var/lib/tomcat7/webapps/ROOT/WEB-INF
  fi
  cp config_root_web.xml /var/lib/tomcat7/webapps/ROOT/WEB-INF/web.xml

  juju-log "III restarting tomcat7"
  service tomcat7 restart
fi
